/*
 * Adara WebCam TimeLapse capture software.
 * Copyright (C) 2012 AdaraProjectTeam
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package adara.cli;

import java.io.File;

import org.apache.commons.cli.CommandLine;

/**
 * Validates the input arguments.
 * @author Andrei Ermicioi (aka erani) <andrei.ermicioi@gmail.com>
 *
 */
public class CLICommandValidator {

	public static void validate(CommandLine cmd) throws CLIException {
		if (cmd.hasOption("help") || cmd.hasOption("list")) {
			return;
		}
		
		if (cmd.hasOption("info")) {
			validateDeviceOption(cmd);
			return;
		}
		
		if (cmd.hasOption("capture")) {
			validateDeviceOption(cmd);
			validateGapOption(cmd);
			validateTimeOption(cmd);
			validateStorageOption(cmd);
		}
	}
	
	private static void validateDeviceOption(CommandLine cmd) 
		throws CLIException {
		
		if (!cmd.hasOption("device")) {
			throw new CLIException("Device sequence is not provided");
		}
		
		int device = -1;
		
		try {
			device = Integer.valueOf(cmd.getOptionValue("device"));
		} catch (NumberFormatException e) {
			throw new CLIException("The device sequence is not number");
		}
		
		if (device < 0) {
			throw new CLIException("The device sequence could not be negative");
		}
	}
	
	private static void validateGapOption(CommandLine cmd) 
		throws CLIException {
		
		if (!cmd.hasOption("gap")) {
			throw new CLIException("Gap is not provided");
		}
		
		int gap = -1;
		
		try {
			gap = Integer.valueOf(cmd.getOptionValue("gap"));
		} catch (NumberFormatException e) {
			throw new CLIException("The gap is not number");
		}
		
		if (gap < 0) {
			throw new CLIException("The gap could not be negative");
		}
	}
	
	private static void validateTimeOption(CommandLine cmd) 
		throws CLIException {
	
		if (!cmd.hasOption("time")) {
			throw new CLIException("Time is not provided");
		}
		
		int duration = -1;
		
		try {
			duration = Integer.valueOf(cmd.getOptionValue("time"));
		} catch (NumberFormatException e) {
			throw new CLIException("The time is not number");
		}
		
		if (duration < 0) {
			throw new CLIException("The time could not be negative");
		}
	}
	
	private static void validateStorageOption(CommandLine cmd)
		throws CLIException {
		
		if (!cmd.hasOption("storage")) {
			throw new CLIException("Storage is not provided");
		}
		
		File storage = new File(cmd.getOptionValue("storage"));
		
		if (!storage.exists() || !storage.isDirectory()) {
			throw new CLIException("Storage does not exist or is not a directory");
		}
	}
}